﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bll;
using Model;
using System.Data;

namespace Accounting.admin.Service
{
    public partial class ServiceRevise : System.Web.UI.Page
    {
        BllService b = new BllService();
        ModelPublic m = new ModelPublic();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (Session["Name"] != null && Session["Type"] != null)
                {
                    txtAuthor.Text = Session["Name"].ToString();

                    UserType.Text = Session["Type"].ToString();

                    if (UserType.Text == "0")
                    {
                        Release.Visible = true;
                    }
                    else
                    {
                        Release.Visible = false;
                    }
                    if (Request.QueryString["Id"] != null)
                    {
                        m.Id = Request.QueryString["Id"].ToString();
                        lblId.Text = Request.QueryString["Id"].ToString();
                        SelectBind();
                        ContentBind();
                    }
                }
                else
                {
                    ASubmit.Disabled = false;
                    btnReturn.Enabled = false;
                }
            }
        }

        public void SelectBind()
        {

            DataTable nav = b.Nav_Inquire_All();

            foreach (DataRow my in nav.Rows)
            {
                if (my["Id"].ToString() == "17")
                {
                    H2_Title.InnerText = "修改：" + my["Name"].ToString();
                }
            }
        }

        public void ContentBind()
        {
            m.Id = lblId.Text;
            DataTable dt = b.Service_Inquire_Single(m);

            foreach (DataRow my in dt.Rows)
            {
                if(my["Type"].ToString()=="0")
                {
                    ServiceAdress.Checked = true;
                    DivServiceAdress.Style["display"] = "block";
                    DivServiceContent.Style["display"] = "none";
                }
                if (my["Type"].ToString() == "1")
                {
                    ServiceContent.Checked = true;
                    DivServiceAdress.Style["display"] = "none";
                    DivServiceContent.Style["display"] = "block";
                }
                txtTitle.Text = my["Title"].ToString();
                txtAuthor.Text = my["Author"].ToString();
                txtNumber.Text = my["Number"].ToString();
                txtDateTime.Text = my["DateTime"].ToString();
                Content.Text = my["_Content"].ToString();
                lblFileName.Text = my["FileName"].ToString();
                lblFileRoute.Text = my["FileRoute"].ToString();
                lblType.Text = my["Type"].ToString();
                txtAdress.Text = my["Adress"].ToString();

                if (my["Release"].ToString() == "True")
                    Release.Checked = true;
                else
                    Release.Checked = false;
                
            }
        }

        protected void btnRevise_Click(object sender, EventArgs e)
        {
            string folderRoute = "";
            m.Id = "'" + lblId.Text+"'" ;
            m.Title = "'" + txtTitle.Text.Trim() + "'";
            m.DateTime = "'" + txtDateTime.Text.Trim() + "'";
            m.Author = "'" + txtAuthor.Text.Trim() + "'";
            if (Content.Text == "")
                m.Content = "NULL";
            else
                m.Content = "'" + Content.Text + "'";
            m.Number = txtNumber.Text.Trim();
            m.NavId = "17";
            m.Type = "'" + lblType.Text + "'";

            if (txtAdress.Text == "")
                m.Adress = "NULL";
            else
                m.Adress ="'"+ txtAdress.Text.Trim() + "'";
            
            if (UserType.Text == "0")
            {
                if (Release.Checked == true)
                    m.Release = "'True'";
                else
                    m.Release = "'False'";
            }
            else
            {
                m.Release = "'false'";
            }

            if (ReleaseFile.HasFile)
            {
                string Upname = ReleaseFile.FileName;

                int Pointposition = 0;
                string FileName = "";
                string suffix = "";

                for (int i = 0; i < Upname.Length; i++)
                {
                    if (Upname[i] == '.')
                    {
                        Pointposition = i;
                        break;
                    }
                }

                for (int i = 0; i < Pointposition; i++)
                {
                    FileName += Upname[i];
                }

                for (int i = Pointposition; i < Upname.Length; i++)
                {
                    suffix += Upname[i];
                }

                m.DateTime = txtDateTime.Text.Trim();

                int num = b.Service_Count_File_Number(m);

                string Time = System.DateTime.Now.ToString("yyyyMMdd");
                folderRoute = "InformationFile/" + Time + (num + 1).ToString() + suffix;

                m.FileName = "'" + ReleaseFile.FileName+ "'" ;
                m.FileRoute = "'" + folderRoute+ "'" ;
            }
            else
            {
                if (lblFileName.Text == "")
                    m.FileName = "NULL";
                else
                    m.FileName ="'"+ lblFileName.Text+ "'" ;
                if (lblFileRoute.Text == "")
                    m.FileRoute = "NULL";
                else
                    m.FileRoute = "'" + lblFileRoute.Text+ "'" ;
            }




            string message = b.Service_Modify(m);
            if (message == "1")
            {
                Response.Write("<script>alert('修改成功！')</script>");
                
                //文件上传
                if (m.FileName != "NULL" && m.FileName!="'"+lblFileName.Text+"'")
                {
                    try
                    {
                        ReleaseFile.SaveAs(Server.MapPath("../../" + folderRoute));
                    }
                    catch (Exception ex)
                    {
                        Response.Write("<script>alert('上传失败:" + ex.Message + "')</script> ");
                    }
                }
                ContentBind();
            }
            else
            {
                Response.Write("<script>alert('" + message + "')</script>");
            }

        }

        private void ContentVerification(string message)
        {
            Response.Write("<script>alert('" + message + "')</script>");
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["type"] != null)
            {
                if (Request.QueryString["type"].ToString() == "0")
                    Response.Redirect("./ServiceAdmin.aspx");
                else
                    Response.Redirect("./ServiceUnpublished.aspx");
            }
        }
    }
}